package view;

import data.User;
import java.util.*;

public class UserUi extends User {
    // 用户登录的方法
    public static void login(ArrayList<data.User> list) {
        Scanner sc=new Scanner(System.in);
        for (int i=0;i<5;i++) {
            System.out.println("请输入用户名");
            String username=sc.next();
            boolean flag= data.User.contains(list,username);
            if(!flag){
                System.out.println("用户名"+username+"未注册，请先注册再登录");
                return;
            }
            System.out.println("请输入密码");
            String password=sc.next();

            while (true) {
                String rightCode=getCode();
                System.out.println("验证码:"+rightCode);
                System.out.println("请输入验证码");
                String code=sc.next();
                if(code.equalsIgnoreCase(rightCode)){
                    System.out.println("验证码正确");
                    break;
                }
                else {
                    System.out.println("验证码错误");
                }
            }
            data.User userInfo=new data.User(username,password,null,null);
            boolean result=checkUserInfo(list,userInfo);
            if(result){
                System.out.println("登录成功，欢迎使用购物系统");
                break;
            }
            else{
                System.out.println("登录失败，用户名或密码错误");
                if(i == 4){
                    System.out.println("当前账号"+username+"被锁定，请等待5分钟再尝试");
                    return;
                }
                else{
                    System.out.println("用户名或密码错误，还剩下"+(4-i)+"次机会");
                }
            }
        }


    }
    // 检查用户信息是否正确的方法
    public static boolean checkUserInfo(ArrayList<data.User>list, data.User userInfo){
        for(int i=0;i<list.size();i++) {
            data.User user = list.get(i);
            if (user.getUsername().equals(userInfo.getUsername()) &&
                    user.getPassword().equals(userInfo.getPassword())) {
                return true;
            }
        }
        return false;

    }
    // 用户注册的方法
    public static void register(ArrayList<data.User> list) {
        Scanner sc = new Scanner(System.in);
        String username;
        while (true) {
            System.out.println("请输入用户名");
            username=sc.next();
            boolean flag1 = checkUsername(username);
            if (!flag1) {
                System.out.println("用户名格式错误，请重新输入");
                continue;
            }
            boolean flag2 = data.User.contains(list, username);
            if (flag2) {
                System.out.println("用户名" + username + "已存在，请重新输入");
            } else {
                System.out.println("用户名" + username + "可用");
                break;
            }
        }
        String password;
        while (true) {
            System.out.println("请输入要注册的密码（密码长度大于8个字符，" +
                    "必须是大小写字母、数字和标点符号的组合。");
            password=sc.next();
            boolean flag3=checkPassword(password);
            if(!flag3){
                System.out.println("密码格式错误，请按照要求输入");
                continue;
            }
            System.out.println("请再次输入要注册的密码");
            String againpassword =sc.next();
            if(!password.equals(againpassword)){
                System.out.println("两次密码输入不一致，请重新输入");

            }
            else {
                System.out.println("两次密码一致，继续录入");
                break;
            }
        }
        String phonenumber;
        while (true) {
            System.out.println("请输入手机号");
            phonenumber=sc.next();
            boolean flag5=checkPhoneNumbeer(phonenumber);
            if(flag5){
                System.out.println("手机号格式正确。继续录入");
                break;
            }else{
                System.out.println("手机号格式有误，请重新输入");
            }
        }
        String email;
        System.out.println("请输入邮箱地址");
        email=sc.next();
//        boolean flag6=checkemail(email);
        data.User u=new data.User(username,password,phonenumber,email);
        list.add(u);
        System.out.println("注册成功");


    }
    // 检查电子邮件地址格式是否正确的方法
    private static boolean checkemail(String email) {
        if(email.matches("^[A-Za-z_]+[0-9]*@(([a-zA-Z0-9]-*)+\\.){1,3}"
                + "[a-zA-Z\\-]+" )){
            return true;
        }
        return false;
    }
    // 检查电话号码格式是否正确的方法
    private static boolean checkPhoneNumbeer(String phonenumber) {
        if(phonenumber.length()!=11){
            return false;
        }
        if(phonenumber.startsWith("0")){
            return false;
        }
        if(phonenumber.matches("[0-9]{11}")){
            return true;
        }
        return false;
    }
    // 检查用户名格式是否正确的方法
    public static boolean checkUsername(String username){
        return username.length() >= 5;
    }
    // 检查密码格式是否正确的方法
    private static boolean checkPassword(String password) {
        if(password.length()<8){
            return false;
        }
        String regex="[a-zA-Z\\d\\p{Punct}|.]+";
        if(password.matches(regex)){
            return true;
        }

        return false;
    }
    // 生成验证码的方法
    static String getCode(){
        ArrayList<Character>list=new ArrayList<>();
        for (int i=0;i<26;i++){
            list.add((char)('a'+i));
            list.add((char)('A'+i));
        }
        StringBuilder ss=new StringBuilder();
        Random r=new Random();
        for(int i=0;i<4;i++){
            int index =r.nextInt(list.size());
            char c=list.get(index);
            ss.append(c);
        }
        int number=r.nextInt(10);
        ss.append(number);
        char[]arr =ss.toString().toCharArray();
        int randomIndex=r.nextInt(arr.length);
        char temp =arr[randomIndex];
        arr[randomIndex]=arr[arr.length-1];
        arr[arr.length-1]=temp;


        return new String(arr);
    }
    // 用户忘记密码的方法
    public static void forgetPassword(ArrayList<data.User> list) {
        Scanner sc1= new Scanner(System.in);
        Scanner sc2= new Scanner(System.in);
        System.out.println("请输入用户名和邮箱地址");
        String username =sc1.next();
        String email =sc2.next();
        boolean flag= data.User.contains(list,username);
        if(!flag){
            System.out.println("当前用户"+username+"未注册，请先注册");
            return;
        }
        int index=findIndex(list,username);
        data.User user=list.get(index);
        if(!user.getEmail().equalsIgnoreCase(email)){
            System.out.println("邮箱地址有误，无法传递密码");
            return;
        }
        String newPassword = generateRandomPassword();
        System.out.println("新密码已发送到邮箱： " + email);
        System.out.println("请使用新密码登录： " + newPassword);
        while (true) {
            System.out.println("请输入新的密码（区分大小写）");
            String againPassword = sc1.next();
            if (newPassword.equals(againPassword)) {
                System.out.println("密码输入正确");
                break;
            }
            else {
                System.out.println("与新密码不一致，请重新输入");
            }
        }
           user.setPassword(newPassword);
    }

    // 查找用户在列表中的索引的方法
    private static int findIndex(ArrayList<data.User> list, String username) {
        for(int i=0;i<list.size();i++){
            data.User user=list.get(i);
            if(user.getUsername().equals(username)){
                return i;
            }
        }
        return -1;
    }
    // 生成随机密码的方法
    public static String generateRandomPassword() {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        int passwordLength = 8;
        Random random = new Random();
        StringBuilder password = new StringBuilder();

        for (int i = 0; i < passwordLength; i++) {
            int index = random.nextInt(characters.length());
            password.append(characters.charAt(index));
        }

        return password.toString();
    }
    // 修改密码的方法
    public static void changePassword(ArrayList<data.User> list) {
        for (int i = 0; i < list.size(); i++) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入您的用户名");
            String username = sc.next();
            User user=list.get(i);
            String password;
            if(Objects.equals(username, user.getUsername())){

                while (true) {
                    System.out.println("请输入新的密码（密码长度大于8个字符，" +
                            "必须是大小写字母、数字和标点符号的组合。");
                    password=sc.next();
                    boolean flag3=checkPassword(password);
                    if(!flag3){
                        System.out.println("密码格式错误，请按照要求输入");
                        continue;
                    }
                    System.out.println("请再次输入修改后的密码");
                    String againpassword =sc.next();
                    if(!password.equals(againpassword)){
                        System.out.println("两次密码输入不一致，请重新输入");

                    }
                    else {
                        System.out.println("两次密码一致，修改成功！");
                        break;
                    }
                }
                    break;
            }
            else{
                System.out.println("修改密码失败，用户名错误");
                break;
            }
        }
    }
}
